package com.xiaotu.common.util;

public class BaseMsgUtils {
	
	public static String birthString(String str){
		
		String[] save = new String[]{"0","1","2","3","4","5","6","7","8","9","年","月","日","-",".","/"};
		
		if(str != null && !str.equals("")){
			str = str.replace("  ", "");
			str = str.replace(" ", "");
			String[] checkStr = str.split("");
			
			while(str.contains("(") && str.contains(")")){
				str = str.replace(str.substring(str.indexOf("("), str.indexOf(")")+1), "");
			}
			
			while(str.contains("（") && str.contains("）")){
				str = str.replace(str.substring(str.indexOf("（"), str.indexOf("）")+1), "");
			}
			
			//去掉无意义的字符串
			for(int i=0;i<checkStr.length;i++){
				int count = 0;
				for(int j=0;j<save.length;j++){
					count++;
					if(checkStr[i].equals(save[j])){
						break;
					}else if(count == save.length){
						str = str.replace(checkStr[i], "");
					}
				}
			}
			
			if(str.contains("日")){
				str = str.substring(0,str.indexOf("日")+1);
			}
			
			while(str.contains("年年")){
				str = str.replace("年年", "年");
			}
			
			str = str.replace("年", "-").replace("月", "-").replace("日", "").replace("/", "-").replace(".", "-");
			while(str.contains("--")){
				str = str.replace("--", "-");
			}
			
			while(str.endsWith("-")){
				str = str.substring(0,str.length()-1);
			}
			
			String[] result = str.split("-");
			
			if(result.length>1){
				String aa = result[1];
				if(aa.length()==1){
					aa = "0"+aa;
				}
				String cc = result[0];
				if(cc.length()==1){
					cc = "0"+cc;
					result[0] = cc;
				}
				result[1] = aa;
			}
			
			if(result.length>2){
				String bb = result[2];
				if(bb.length() == 1){
					bb = "0"+bb;
				}
				result[2] = bb;
			}
			
			if(result.length == 2){
				str = result[0]+"-"+ result[1];
			}
			
			if(result.length == 3){
				str = result[0]+"-"+ result[1]+"-"+ result[2];
			}
			
			if(str.length()>10){
				str = str.substring(0,10);
			}
			
			return str;
		}else{
			return null;
		}
	}
	
	public static void main(String[] args) {
		
	//	String a = BaseMsgUtils.birthString("上映时间：1983-02-25（中国香港）");
		String a = BaseMsgUtils.birthString("3月16日（中国香港）");
		System.out.println(a);
	}
}
